//
//  SOChooseDateHeader.m
//  Calendar
//
//  Created by 王文娟 on 16/7/13.
//  Copyright © 2016年 EJU. All rights reserved.
//

#import "SOChooseDateHeader.h"

@interface SOChooseDateHeader()

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, weak) UILabel *yearLabel;

@property (nonatomic, weak) UIView *weekTitleView;

@property (nonatomic, weak) UIButton *todayButton;

@end

@implementation SOChooseDateHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        UILabel *yearLabel = [[UILabel alloc]init];
        
        yearLabel.textAlignment = NSTextAlignmentCenter;
        
        _yearLabel = yearLabel;
        
        [self addSubview:yearLabel];
        
        UIButton *button = [[UIButton alloc]init];
        
        [button addTarget:self action:@selector(todayClick) forControlEvents:UIControlEventTouchUpInside];
        
        [button setTitle:@"今天" forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        
        button.layer.cornerRadius = 5;
        
        button.clipsToBounds = YES;
        
        button.layer.borderColor = [UIColor blueColor].CGColor;
        
        button.layer.borderWidth = 1;
        
        self.todayButton = button;
        
        [self addSubview:button];

        
        UIView *weekTitleView = [[UIView alloc]init];
        
        _weekTitleView = weekTitleView;
        
        [self addSubview:weekTitleView];
        
        for (NSString *str in self.titleArray) {
            
            UILabel *label = [[UILabel alloc]init];
            
            label.textAlignment = NSTextAlignmentCenter;
            
            label.text = str;
            
            [weekTitleView addSubview:label];
        }
        
    }
    return self;
}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.yearLabel.frame = (CGRect){0,6,self.frame.size.width,self.frame.size.height/2.0-6};
    
    self.todayButton.frame = (CGRect){self.frame.size.width- 55,5,45,24};
    
    self.weekTitleView.frame = (CGRect){0,CGRectGetMaxY(self.yearLabel.frame),self.frame.size.width,self.frame.size.height/2.0};
    
    NSInteger i = 0;
    
    CGFloat W = self.frame.size.width/self.titleArray.count;
    
    CGFloat H = self.frame.size.height/2.0;
    
    for (UIView *subView in self.weekTitleView.subviews) {
        
        subView.frame = (CGRect){i*W, 0, W, H};
        
        i++;
        
    }
    
}

-(void)todayClick{
    
    if([self.deledate respondsToSelector:@selector(didClickTodayInChooseDateHeader:)]){
        
        [self.deledate didClickTodayInChooseDateHeader:self];
        
    }
    
}

-(NSArray *)titleArray{
    
    if(_titleArray==nil){
        
        NSArray *titleArray = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
        
        self.titleArray =  titleArray;
    }
    
    return _titleArray;
    
}

-(void)setYearText:(NSString *)yearText{

    _yearText = yearText;
    
    self.yearLabel.text = yearText;

}

@end
